package edu.csumb.itcd.teammario;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Random;




/**
 *
 * @author Team Mario
 *
 */
public class Enemy {
    
    protected int money;
    protected int speed;
    protected Point2D enemyPosition;
    protected int currentHealth, maxHealth;
    protected int weaponType;
    protected int gwidth, gheight;
    protected int width, height;
    protected boolean isAlive = false;
    protected boolean visible = false;
    protected int damage;
    protected Weapon weapon;
    protected Color enemyc;
    protected Random avoid = new Random();
    protected Poison poisonReceived;
    protected double distY;
    protected double distX;
    protected double vectorY;
    protected double vectorX;
    protected double moveY;
    protected double moveX;
    
    public int getDamage() {
        return damage;
    }

    public void setDamage(int damage) {
        this.damage = damage;
    }

    public void setPoisonReceived(Poison poison)
    {
    	this.poisonReceived=poison;
   	//System.out.println("newpoison");
    }
    
    public Enemy(Point spawnPoint)
    {
        
        maxHealth = 30;
        currentHealth = 30;
        money = 1;
        speed = 2;
        weaponType = 1;
        enemyPosition = spawnPoint;
        isAlive = true;
        width = 20;
        height = 20;
        damage = 1;
        poisonReceived = new Poison();
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public Point2D getEnemyPosition() {
        return enemyPosition;
    }

    public void setEnemyPosition(Point2D enemyPosition) {
        this.enemyPosition = enemyPosition;
    }

    public int getCurrentHealth() {
        return currentHealth;
    }

    public void setCurrentHealth(int currentHealth) {
        this.currentHealth = currentHealth;
    }

    public int getMaxHealth() {
        return maxHealth;
    }

    public void setMaxHealth(int maxHealth) {
        this.maxHealth = maxHealth;
    }

    public int getWeaponType() {
        return weaponType;
    }

    public void setWeaponType(int weaponType) {
        this.weaponType = weaponType;
    }
    
    public void moveAi(int playerX, int playerY)
    {
//            if ((enemyPosition.x + width / 2) - 5 < playerX)
//            {
//                enemyPosition.x = enemyPosition.x + speed ;
//            }
//            else if ((enemyPosition.x + width / 2) - 5 > playerX)
//            {
//                enemyPosition.x = enemyPosition.x - speed;
//            }
//            if ((enemyPosition.y + height /2) - 5 > playerY)
//            {
//                enemyPosition.y = enemyPosition.y - speed;
//            }
//            else if((enemyPosition.y + height /2) - 5 < playerY)
//            {
//                enemyPosition.y = enemyPosition.y + speed;
//            }
    	vectorY = playerY - (height/2) + (Player.getHeight()/2) - enemyPosition.getY();
        vectorX = playerX - (width/2) + (Player.getWidth()/2) - enemyPosition.getX();

        //The dist variables need to be positive for the trig equations.
        distY = Math.abs(vectorY);
        distX = Math.abs(vectorX);

        //Gives the vectors a value of 1 or -1.
        vectorY /= distY;
        vectorX /= distX;

        /**
         * tan(x) = opposite/adjacent
         * tan(x) = distY/distX
         * x = arctan(distY/distX)
         *
         * cos(x) = adjacent/hypotenuse
         * cos(x) = moveX/speed
         * moveX = speed * cos(x)
         * moveX = speed * cos(arctan(distY/distX))
         */
        if (distX == 0 && distY == 0)
        {
            moveY = 0;
            moveX = 0;
        } else if (distX == 0)
        {
        	moveY = speed * vectorY;
        	moveX = 0;
        } else if (distY == 0)
        {
        	moveY = 0;
        	moveX = speed * vectorX;
        } else
        {
            moveY = speed * Math.sin(Math.atan(distY/distX));
            moveX = speed * Math.cos(Math.atan(distY/distX));
//            moveY = Math.rint(moveY) * vectorY;
//            moveX = Math.rint(moveX) * vectorX;
            moveY = moveY * vectorY;
            moveX = moveX * vectorX;
        }
        enemyPosition.setLocation(enemyPosition.getX() + moveX, enemyPosition.getY() + moveY);
        
            if (currentHealth <= 0)
            {
                isAlive = false;
                LeaderBoard.incrementKillCount();
            }
            
            if (enemyPosition.getX() > 0 && enemyPosition.getX() + width < 800 - 5 && enemyPosition.getY() > 55 && enemyPosition.getY() + height < 600 - 30)
            {
            	visible = true;
            }
    }

    public void flocking(ArrayList<Enemy> enemyArray)
    {
    	for (Enemy otherEnemy : enemyArray)
    	{
    		if (getEnemyImage().intersects(otherEnemy.getEnemyImage()) && this != otherEnemy)
    		{
    			if (enemyPosition.getX() >= otherEnemy.getEnemyPosition().getX())
    			{
    				enemyPosition.setLocation(enemyPosition.getX() + avoid.nextInt(2), enemyPosition.getY());
    			} else
    			{
    				enemyPosition.setLocation(enemyPosition.getX() - avoid.nextInt(2), enemyPosition.getY());
    			}
    			if (enemyPosition.getY() >= otherEnemy.getEnemyPosition().getY())
    			{
    				enemyPosition.setLocation(enemyPosition.getX(), enemyPosition.getY() + avoid.nextInt(2));
    			} else
    			{
    				enemyPosition.setLocation(enemyPosition.getX(), enemyPosition.getY() - avoid.nextInt(2));
    			}
    			if (this.poisonReceived.isCurrentlyPoisoned() == true && this.poisonReceived.getInfectionCount() < this.poisonReceived.getInfectionMax() && otherEnemy.poisonReceived.isCurrentlyPoisoned() == false)
    			{
    				this.poisonReceived.setInfectionCount(this.poisonReceived.getInfectionCount() + 1);
    				otherEnemy.setPoisonReceived(this.poisonReceived);
    				
    			}
    		}
    	}
    }
    
            
    
    public int getGwidth() {
        return gwidth;
    }
    public boolean isActive()
    {
        return isAlive;
    }

    public void setGwidth(int gwidth) {
        this.gwidth = gwidth;
    }

    public int getGheight() {
        return gheight;
    }

    public void setGheight(int gheight) {
        this.gheight = gheight;
    }
    public int getWidth()
    {
        return width;
    }
    public int getHeight()
    {
        return height;
    }

    public boolean isVisible() {
		return visible;
	}

	public void paintEnemy(Graphics2D g2)
    {
    	if (isAlive == true)
        {
            //temp is to make sure the number is in bounds for the new Color
            int temp = (currentHealth*165)/40;
            if (temp < 0)
                temp = 0;
            else if (temp > 255)
                temp = 255;
            
            enemyc = new Color(225,temp,0);

            g2.setColor(enemyc);
            if (poisonReceived.isCurrentlyPoisoned() == true)
            {
            	g2.setColor(Color.GREEN);
            }
            else
            {
            	g2.setColor(enemyc);
            }
            g2.fill(new Rectangle2D.Double(enemyPosition.getX(), enemyPosition.getY(), width, height));
            g2.setColor(Color.black);
            g2.draw(new Rectangle2D.Double(enemyPosition.getX(), enemyPosition.getY(), width, height));  
        }
    }
    
    public Weapon getWeapon() {
		return weapon;
	}

	public void setWeapon(Weapon weapon) {
		this.weapon = weapon;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public Rectangle2D getEnemyImage()
    {
    	return new Rectangle2D.Double(enemyPosition.getX(), enemyPosition.getY(), width, height);
    }
    
}

